package com.ngc.mall.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.domain.SpuDomain;
import com.ngc.mall.domain.SpuSpecDomain;
import com.ngc.mall.dto.SpuDTO;
import com.ngc.mall.dto.SpuFilterDTO;
import com.ngc.mall.param.CategoryParam;
import com.ngc.mall.service.ISpuService;
import com.ngc.mall.service.ISpuSpecService;
import com.ngc.mall.vo.PagingVO;
import com.ngc.mall.vo.SpuOverviewVO;
import com.ngc.mall.vo.SpuSimplifyVO;
import com.ngc.mall.vo.SpuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
/**
 * 商品
 *
 * @author: chenxiaolin
 * @date 2023/4/4 8:56
**/
@RestController
@RequestMapping("/spu")
@Api(tags = "商品")
public class SpuController {
    @Resource
    private ISpuService service;

    @Resource
    private ISpuSpecService spuSpecService;

    @GetMapping("/overview")
    @ApiOperation("商品总览")
    public ResponseResult<SpuOverviewVO> spuOverview(){
        return ResponseResult.success(service.getSpuOverview());
    }

    @PostMapping("")
    @ApiOperation(value = "新增商品")
    public ResponseResult<Long> createSpu(@RequestBody SpuDTO dto){
        return ResponseResult.success(this.service.createSpu(dto));
    }


    @PutMapping("/{id}")
    @ApiOperation("更新商品")
    public Boolean updateSpuById(@PathVariable Long id,@RequestBody SpuDTO dto){
        return service.updateSpuById(id,dto);
    }


    @GetMapping("/list")
    @ApiOperation(value = "获取所有商品信息")
    public ResponseResult<List<SpuDomain>> selectAllSpu(){
        return ResponseResult.success(this.service.selectAllSpu());
    }


    @GetMapping("/id/{id}")
    @ApiOperation(value = "根据id获取spu详细信息")
    public ResponseResult<SpuVO> getSpuDetailById(@PathVariable Long id){
         return ResponseResult.success(this.service.getDetailById(id));
    }



    @GetMapping("/detail/simplify/{id}")
    @ApiOperation(value = "根据id获取简化spu信息")
    public SpuSimplifyVO getSpuSimplifyById(@PathVariable Long id){
        return this.service.getSimplifyById(id);
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除商品")
    public ResponseResult deleteSpuById(@PathVariable Long id){
        return ResponseResult.auto(service.removeById(id));
    }


    @PutMapping("/offline/{id}")
    @ApiOperation("下架商品")
    public ResponseResult updateToOffline(@PathVariable Long id){
        return ResponseResult.auto(service.updateSpuStatus(false,id));
    }


    @PutMapping("/online/{id}")
    @ApiOperation("上架商品")
    public ResponseResult updateToOnline(@PathVariable Long id){
        return ResponseResult.auto(service.updateSpuStatus(true,id));
    }


    @GetMapping("/{id}/specs")
    @ApiOperation("查询指定id下的规格信息")
    public void getSpecsById(@PathVariable Long id){
          List<SpuSpecDomain> list=spuSpecService.list(Wrappers.<SpuSpecDomain>lambdaQuery().eq(SpuSpecDomain::getSpuId,id));
        System.out.println(list);
    }



    @PostMapping("/search")
    @ApiOperation("模糊搜索")
    public ResponseResult<PagingVO<SpuDomain>> search(@RequestBody SpuFilterDTO dto, @RequestParam(name = "start", defaultValue = "0")
            Integer start,
                                                      @RequestParam(name = "count", defaultValue = "10")
                                                  Integer count){
        return ResponseResult.success(service.search(dto,start,count));
    }

    @PostMapping("/by/category")
    @ApiOperation("根据分类id分页获取商品信息(包含子分类下的所有商品信息）")
    public ResponseResult<PagingVO<SpuDomain>> getSpuListByCategoryId(@RequestBody CategoryParam categoryParam,
                                                                      @RequestParam(name = "start", defaultValue = "0")
                                                                              Integer start,
                                                                      @RequestParam(name = "count", defaultValue = "8")
                                                                              Integer count){
        return ResponseResult.success(this.service.getSpuListByCategoryId(categoryParam.getCategoryIdList(),start,count));
    }
}
